package com.hspedu.newData.dmsxl.图;
/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-28 
 * @Version: 1.0
 */

public class LC695岛屿的最大面积 {

    int max = 0;
    int count = 0;
    private int[][] dir = new int[][]{{0,1},{1,0},{-1,0},{0,-1}};

    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1){
//                    count = 1;
                    dfs(grid,i,j);
                    max = Math.max(max,count);
                    count = 0;
                }
            }
        }
        return max;
    }

    public void dfs(int[][] grid,int x,int y){
        if (x<0||y<0||x>= grid.length||y>=grid[0].length||grid[x][y]!=1){
            return;
        }
        count++;
        // todo 需要将已经遍历的转成0,否则会不停的递归
        grid[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            dfs(grid,nextX,nextY);
        }
    }

}
